home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The PC-SIG Library 9
/
The PC-SIG Library on CD ROM - Ninth Edition.iso
/
001_100
/
DISK0011
/
DISK0011.ZIP
/
HEAPSORT.BAS
< prev
next >
Wrap
BASIC Source File
|
1982-04-07
|
2KB
|
80 lines
100 CLEAR 5000
110 CLS ' Sort Program Heap Sort
120 DIM N(150),C$(150)
130 PRINT"type C for character string sort"
140 PRINT"Type N for numer sort"
150 INPUT W$
160 N=0:PRINT:PRINT
170 IF W$="n" THEN 480 ' Goto to Number sort
180 IF W$<>"c" THEN 130
190 REM==================== Begin Character Sort
200 GOSUB 720 : INPUT S$
210 N=N+1
220 INPUT C$(N)
230 IF C$(N)<>S$ THEN 210
240 N=N-1:PRINT
250 L=INT(N/2)+1
260 N1=N
270 IF L=1 THEN 310
280 L=L-1
290 A$=C$(L)
300 GOTO 350
310 A$=C$(N1)
320 C$(N1)=C$(1)
330 N1=N1-1
340 IF N1=1 THEN 440
350 J=L
360 I=J
370 J=2*J
380 IF J=N1 THEN 400
390 IF J>N1 THEN 420
392 IF C$(J)=>C$(J+1) THEN 400
396 J=J+1
400 IF A$=>C$(J) THEN 420
410 C$(I)=C$(J)
415 GOTO 360
420 C$(I)=A$
425 GOTO 270
440 C$(1)=A$
450 FOR I=1 TO N
453 PRINT C$(I)
456 NEXT I
460 GOTO 130
470 REM ================ Start of numeric sort
480 GOSUB 720
483 INPUT S
486 PRINT
490 N=N+1
493 INPUT N(N)
496 IF N(N)<>S THEN 490
500 N=N-1
505 PRINT
520 L=INT(N/2)+1
525 N1=N
530 IF L=1 THEN 550
540 L=L-1
543 A=N(L)
546 GOTO 590
550 A=N(N1)
555 N(N1)=N(1)
560 N1=N1-1
570 IF N1=1 THEN 680
590 J= L
600 I=J
605 J=2*J
610 IF J=N1 THEN 640
620 IF J>N1 THEN 660
630 IF N(J)<N(J+1) THEN J=J+1
640 IF A>N(J) THEN 660
650 N(I) = N(J)
655 GOTO 600
660 N(I)=A
665 GOTO 530
680 N(1)=A
690 FOR I=1 TO N
693 PRINT N(I)
696 NEXT I
700 GOTO 130
720 PRINT"Enter a stop code to indicate the end of list"
740 RETURN
OR I=1 TO